﻿using System;
using System.Web.UI.WebControls;
using MaLu.Common;
using MaLu.Data;
using System.Data;
using System.Text;
using Wicresoft.Framework.Organization;
using MaLu.Framework.Common;

namespace MaluJg.web.HonestRisk
{
    public partial class HEdit :HPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "风险防范监管添加";
            ScriptManager1.RegisterAsyncPostBackControl(lbtnSave);
           
            
            if (!IsPostBack)
            {
                Department mainD = OrganizationHelper.GetDepartmentInfo(CurrentUser.MainDepartmentID);
                string[] ids = mainD.IDPath.Split('/');
                if (ids.Length >= 4)
                    DepartmentID = new Guid(ids[3]);
                else
                    DepartmentID = new Guid(ids[ids.Length - 1]);

                Binddata();
              
             
            }

        }
        private Guid RecordID
        {
            get
            {
                Guid gd = Guid.NewGuid();
                if (Request["ID"] != null)
                    gd = new Guid(Request["ID"]);
                else
                {
                    if (ViewState["ID"] != null)
                        gd = (Guid)ViewState["ID"];


                }
                if (ViewState["ID"] == null)
                    ViewState["ID"] = gd;
                return gd;
            }
            set
            {
                ViewState["ID"] = value;
            }

        }
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            Department dept = OrganizationHelper.GetDepartmentInfo(DepartmentID);
            if (dept != null)
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat("declare @id uniqueidentifier; select @id=id from [HRisk] where id='{0}';", RecordID);

                sql.AppendLine("if (@@rowcount=0) begin ");
                sql.AppendFormat(@"INSERT INTO [HRisk]
           ([ID],[Dept],[Position],[Responsibilities],[PatentRisk],[RiskLevel],
           [Precautions],[Others],[DepartmentID],[DepartmentDspName],[Creator]
           ,[CreateTime],[ModifyTime],[Writor],[WriteTime])   VALUES
           ('{0}',{1},{2},{3},{4},{5},{6},{7},'{8}','{9}','{10}',getdate(),NULL,{11},{12});
            end else
               begin 
                UPDATE [HRisk]
   SET [Dept] = {1}  ,[Position] = {2}  ,[Responsibilities] ={3}  ,[PatentRisk] ={4}
      ,[RiskLevel] = {5} ,[Precautions] = {6},[Others]={7},[ModifyTime] =getdate(),[Writor]={11},[WriteTime]={12}
 WHERE ID='{0}'", RecordID, Util.ReturnSqlValue(tbDept.Text)
              , Util.ReturnSqlValue(tbPosition.Text), Util.ReturnSqlValue(tbResponsibilities.Text), Util.ReturnSqlValue(tbPatentRisk.Text)
              , Util.ReturnSqlValue(tbLevel.Text), Util.ReturnSqlValue(tbPrecautions.Text), Util.ReturnSqlValue(tbOthers.Text),
              CurrentUser.MainDepartmentID, dept.DisplayName, CurrentUser.Name, Util.ReturnSqlValue(tbWritor.Text), Util.ReturnSqlValue(tbWriteTime.Text));
                sql.AppendLine("end   ");
                SqlHelper.Instance.ExecuteNonQuery(Provider, sql.ToString());
            }

            System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel3, this.GetType(), "save", "alert('成功保存!');window.opener.location.href=window.opener.location.href;window.close();", true);

            UpdatePanel3.Update();
           
        }

        private void Binddata()
        {
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, "select * from [HRisk]  where id ='" + RecordID + "'");
            if (dt.Rows.Count > 0)
            {
                this.Title = "风险防范监管编辑";
                lbtnSave.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                DataRow dr = dt.Rows[0];

                tbDept.Text = dr["Dept"].ToString().Replace("''", "'");
                tbPosition.Text = dr["Position"].ToString().Replace("''", "'");
                tbResponsibilities.Text = dr["Responsibilities"].ToString().Replace("''", "'");
                tbPatentRisk.Text = dr["PatentRisk"].ToString().Replace("''", "'");
                tbLevel.Text = dr["RiskLevel"].ToString().Replace("''", "'");
                tbPrecautions.Text = dr["Precautions"].ToString().Replace("''", "'");
                tbOthers.Text = dr["Others"].ToString().Replace("''", "'");
                tbWritor.Text = dr["Writor"].ToString().Replace("''", "'");
                tbWriteTime.Text = dr["WriteTime"].ToString().Replace("''", "'");            
               
            }
        }
        
    }
}
